/**
 * *****************************************************************
 * @file    adt3102_system.h
 * @author  WuHao(hwu@andartechs.com.cn)
 * @version 1.0.0
 * @date    2020-11-24
 * @brief   
 *                 Copyright (c) 2020, Andar Technologies Inc.
 *                           www.andartechs.com 
 *
 * *****************************************************************
 */
#ifndef __SYSTEM__H
#define __SYSTEM__H


/*------------------------------ include -------------------------------------*/
#include "adt3102_type_define.h"


/*------------------------------ define --------------------------------------*/
/* INT_xxx :xxx interrupt number */
#define INT_UART0        0
#define INT_UART1        1
#define INT_UART2        2
#define INT_TIMER0       3
#define INT_TIMER1       4
#define INT_DUALTIMER    5
#define INT_UARTOVERFLOW 6
#define INT_PMU          7
#define INT_SPI0         8
#define INT_SPI1         9
#define INT_RTC          10
#define INT_I2C0         11
#define INT_I2C1         12
#define INT_FFT          13
#define INT_GPIO         15
#define INT_DMA0         16
#define INT_DMA1         17
#define INT_TRNG         18

/*
ENABLEINT  :enable interrupt
DISABLEINT :disable interrupt
*/
#define ENABLEINT     1
#define DISABLEINT    0

typedef enum
{
    ENABLE  = 1,
    DISABLE = 0
} StateTypedef;


#define ON     1  
#define OFF    0  

#define PIN2        (0X0001<<2)
#define PIN3        (0X0001<<3)
#define PIN4        (0X0001<<4)
#define PIN5        (0X0001<<5)
#define PIN6        (0X0001<<6)
#define PIN7        (0X0001<<7)
#define PIN8        (0X0001<<8)
#define PIN9        (0X0001<<9)
#define PIN10       (0X0001<<10)
#define PIN11       (0X0001<<11)
#define PIN12       (0X0001<<12)
#define PIN13       (0X0001<<13)
#define PIN14       (0X0001<<14)
#define PIN15       (0X0001<<15)
#define PIN16       (0X0001<<16)
#define PIN17       (0X0001<<17)

#define GLEDS       0x3fffc
#define LEDUP       (PIN5|PIN6|PIN7|PIN8|PIN9|PIN10|PIN11|PIN12|PIN13|PIN15)
#define LEDDOWN     (PIN2|PIN6|PIN7|PIN8|PIN9|PIN10|PIN11|PIN12|PIN13|PIN17)
#define LEDLEFT     (PIN3|PIN4|PIN6|PIN8|PIN10|PIN12|PIN14|PIN15|PIN16|PIN17)
#define LEDRIGHT    (PIN2|PIN3|PIN4|PIN5|PIN6|PIN8|PIN10|PIN12|PIN14|PIN16)
#define LEDIN       (PIN6|PIN8|PIN10|PIN12)
#define LEDOUT      (PIN3|PIN4|PIN7|PIN9|PIN11|PIN13|PIN14|PIN16)

#define LEDUPIN     (PIN3|PIN6|PIN7|PIN10|PIN11|PIN16)
#define LEDDOWNIN   (PIN4|PIN8|PIN9|PIN12|PIN13|PIN14)
#define LEDRIGHTIN  (PIN10|PIN11|PIN12|PIN13|PIN14|PIN16)
#define LEDLEFTIN   (PIN3|PIN4|PIN6|PIN7|PIN8|PIN9)

#define LEDUPOUT    (PIN5|PIN8|PIN9|PIN12|PIN13|PIN15)
#define LEDDOWNOUT  (PIN2|PIN6|PIN7|PIN10|PIN11|PIN17)
#define LEDRIGHTOUT (PIN2|PIN3|PIN4|PIN5|PIN6|PIN8)
#define LEDLEFTOUT  (PIN10|PIN12|PIN14|PIN15|PIN16|PIN17)


/*------------------------------ variable ------------------------------------*/
extern volatile int32 g_delayUsFlag;
/*------------------------------ function ------------------------------------*/
/**
 * @brief   switching interrupt .
 *      NMI: Watchdog
 *      0  : Uart0 tx/rx                9  : spi1
 *      1  : Uart1 tx/rx                10 : RTC
 *      2  : Uart3 tx/rx                11 : IIC0
 *      3  : Timer0                     12 : IIC1
 *      4  : Timer1                     13 : FFT
 *      5  : Dualtimer2                 14 : reserve
 *      6  : Uart0/1/2 overflow         15 : gpio
 *      7  : pmu interrupt              16 : dma_int0
 *      8  : spi0                       17 : dma_int1       18 : TRNG
 * @param   interrutNum: interrupt number.
 * @param   enableOrDisenable: on or off total interrupt 1:on 0:off.
 * @return  None.
 * @note
 */
void setInterrupt(int32 interruptNum, int32 enableOrDisenable);

/**
 * @brief   Used to indicate that the vector table is in the
 *          code area or in the RAM area.
 * @param   NVIC_VectTab: The defalut of ADT3102 is 0x00000000.
 * @param   Offset: vector table offset ,The defalut of ADT3102 is 0x8000.
 * @return  None.
 * @note
 */
void MY_NVIC_SetVectorTable(uint32 NVIC_VectTab, uint32 Offset);

/**
 * @brief   system_reset.
 * @param   None.
 * @return  None.
 * @note
 */
void system_reset(void);

/**
 * @brief   The delay realized by dualtimer, the accuracy is in "US".
 * @param   delayUs: the time you want to delay,in us.
 * @param   highClock50M: high clock setting,50M or 125M.
 * @return  None.
 * @note
 */
void delayUs(uint32 delayUs,int32 highClock50M);

/**
 * @brief   switch 125M highclock.
 * @param   None.
 * @return  None.
 * @note
 */
void switch125MHclk(void);

/**
 * @brief   turn on plli.
 * @param   None.
 * @return  None.
 * @note
 */
void turnOnPlli(void);

/**
 * @brief   get range res.
 * @param   fm: .
 * @param   t0: .
 * @param   downSample: .
 * @param   fftPoint: .
 * @return  float.
 * @note
 */
float getRangeRes(uint32 fm, uint32 t0, uint16 downSample, uint16 fftPoint);

/**
 * @brief   initial high clock, sleep config, rtc and calibration, reset phase shifter.
 * @param   None.
 * @return  None.
 * @note
 */
void adt3102DigiInit(void);

/**
 * @brief   initial tx and rx and psr.
 * @param   rx0En: rx0 enable.
 * @param   rx1En: rx1 enable.
 * @param   tx0En: tx0 enable.
 * @param   tx1En: tx1 enable.
 * @param   phaseShftLut0: phase Shift Lut0.
 * @param   phaseShftLut1: phase Shift Lut1.
 * @param   chirpNumber: .
 * @return  None.
 * @note
 */
void adt3102RfInit(int32 rx0En,int32 rx1En,int32 tx0En,int32 tx1En,uint32 phaseShftLut0,uint32 phaseShftLut1,uint32 chirpNumber);

#ifndef COMPATIBLE
/**
 * @brief   initial pa, lna, mixer, tia, pga, hpf, lpf and so on.
 * @param   None.
 * @return  None.
 * @note
 */
void adt3102Analog(void);
#endif

/**
 * @brief   initalization p2~p17.
 * @param   None.
 * @return  None.
 * @note
 */
void ledInit(void);

/**
 * @brief   turn on/off all p2~p17.
 * @param   None.
 * @return  None.
 * @note
 */
void ledAllon(void);
void ledAlloff(void);

/**
 * @brief   led on/off.
 * @param   Pin_number: gpio number.
 * @return  None.
 * @note
 */
void ledOn(unsigned int Pin_number);
void ledOff(unsigned int Pin_number);

/**
 * @brief   led gesture indication.
 * @param   actionType: gesture indication
 *              1:IN 2:OUT 3:LEFT 4:RIGHT 5:UP 6:DOWN.
 * @return  None.
 * @note
 */
void ledDisplay(uint32 actionType);

#endif












